﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TrainingThree_14
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        bool TurnOnOff = false;
        double op1, op2;
        char ch;


        private void FunTurn()
        {
            btn0.Enabled = TurnOnOff;
            btn0.FlatStyle = FlatStyle.Flat;//样式
            btn0.FlatAppearance.BorderSize = 0;//去边线

            btn1.Enabled = TurnOnOff;
            btn1.FlatStyle = FlatStyle.Flat;
            btn1.FlatAppearance.BorderSize = 0;

            btn2.Enabled = TurnOnOff;
            btn2.FlatStyle = FlatStyle.Flat;
            btn2.FlatAppearance.BorderSize = 0;

            btn3.Enabled = TurnOnOff;
            btn3.FlatStyle = FlatStyle.Flat;
            btn3.FlatAppearance.BorderSize = 0;

            btn4.Enabled = TurnOnOff;
            btn4.FlatStyle = FlatStyle.Flat;
            btn4.FlatAppearance.BorderSize = 0;

            btn5.Enabled = TurnOnOff;
            btn5.FlatStyle = FlatStyle.Flat;
            btn5.FlatAppearance.BorderSize = 0;

            btn6.Enabled = TurnOnOff;
            btn6.FlatStyle = FlatStyle.Flat;
            btn6.FlatAppearance.BorderSize = 0;

            btn7.Enabled = TurnOnOff;
            btn7.FlatStyle = FlatStyle.Flat;
            btn7.FlatAppearance.BorderSize = 0;

            btn8.Enabled = TurnOnOff;
            btn8.FlatStyle = FlatStyle.Flat;
            btn8.FlatAppearance.BorderSize = 0;

            btn9.Enabled = TurnOnOff;
            btn9.FlatStyle = FlatStyle.Flat;
            btn9.FlatAppearance.BorderSize = 0;

            btnPoint.Enabled = TurnOnOff;
            btnPoint.FlatStyle = FlatStyle.Flat;
            btnPoint.FlatAppearance.BorderSize = 0;

            btnSign.Enabled = TurnOnOff;
            btnSign.FlatStyle = FlatStyle.Flat;
            btnSign.FlatAppearance.BorderSize = 0;

            btnResult.Enabled = TurnOnOff;
            btnResult.FlatStyle = FlatStyle.Flat;
            btnResult.FlatAppearance.BorderSize = 0;
            btnResult.BackColor = Color.OrangeRed;

            btnAdd.Enabled = TurnOnOff;
            btnAdd.FlatStyle = FlatStyle.Flat;
            btnAdd.FlatAppearance.BorderSize = 0;

            btnSub.Enabled = TurnOnOff;
            btnSub.FlatStyle = FlatStyle.Flat;
            btnSub.FlatAppearance.BorderSize = 0;

            btnMul.Enabled = TurnOnOff;
            btnMul.FlatStyle = FlatStyle.Flat;
            btnMul.FlatAppearance.BorderSize = 0;

            btnDiv.Enabled = TurnOnOff;
            btnDiv.FlatStyle = FlatStyle.Flat;
            btnDiv.FlatAppearance.BorderSize = 0;

            btnBackSpace.Enabled = TurnOnOff;
            btnBackSpace.FlatStyle = FlatStyle.Flat;
            btnBackSpace.FlatAppearance.BorderSize = 0;

            btnClear.Enabled = TurnOnOff;
            btnClear.FlatStyle = FlatStyle.Flat;
            btnClear.FlatAppearance.BorderSize = 0;

            btnClearError.Enabled = TurnOnOff;
            btnClearError.FlatStyle = FlatStyle.Flat;
            btnClearError.FlatAppearance.BorderSize = 0;

            btnPercent.Enabled = TurnOnOff;
            btnPercent.FlatStyle = FlatStyle.Flat;
            btnPercent.FlatAppearance.BorderSize = 0;

            btnSqrt.Enabled = TurnOnOff;
            btnSqrt.FlatStyle = FlatStyle.Flat;
            btnSqrt.FlatAppearance.BorderSize = 0;

            btnSquare.Enabled = TurnOnOff;
            btnSquare.FlatStyle = FlatStyle.Flat;
            btnSquare.FlatAppearance.BorderSize = 0;

            btnReciprocal.Enabled = TurnOnOff;
            btnReciprocal.FlatStyle = FlatStyle.Flat;
            btnReciprocal.FlatAppearance.BorderSize = 0;

            btnFactorial.Enabled = TurnOnOff;
            btnFactorial.FlatStyle = FlatStyle.Flat;
            btnFactorial.FlatAppearance.BorderSize = 0;

            btnOnOff.FlatStyle = FlatStyle.Flat;
            btnOnOff.FlatAppearance.BorderSize = 0;

            txtResult.BringToFront();//将txtResult置于顶层
            txtResult2.SendToBack();//将txtResult2置于底层
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FunTurn();

        }

        private void btnOnOff_Click(object sender, EventArgs e)
        {
            if (TurnOnOff == false)
            {
                TurnOnOff = true;
                btnOnOff.Text = "OFF";
                txtResult.Text = "0";
            }
            else
            {
                TurnOnOff = false;
                btnOnOff.Text = "ON";
                txtResult.Text = "";
            }
            FunTurn();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtResult.Text = "0";
        }

        private void btnClearError_Click(object sender, EventArgs e)
        {
            txtResult.Text = "0";
            return;
        }

        //数字的录入
        private void btnDigital_Click(object sender, EventArgs e)
        {
            txtResult.BringToFront();//将txtResult置于顶层
            txtResult2.SendToBack();//将txtResult2置于底层

            Button bt = (Button)sender;
            if (txtResult.Text != "0")
                txtResult.Text += bt.Text;
            else
            {
                txtResult.Text = bt.Text;
            }
        }

        //BackSpace按键
        private void btnBackSpace_Click(object sender, EventArgs e)
        {
            if (txtResult.Text.Length > 1)
            {
                //textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
                txtResult.Text = txtResult.Text.Remove(txtResult.Text.Length - 1);
            }
            else
            {
                txtResult.Text = "0";
            }
        }

        //求阶乘
        private void btnFactorial_Click(object sender, EventArgs e)
        {
            uint s = 1, n;
            try
            {
                n = Convert.ToUInt32(txtResult.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "错误");
                return;
            }
            for (uint i = n; i > 0; i--)
            {
                s *= i;
            }
            txtResult.Text = s.ToString();
        }

        //求平方根
        private void btnSqrt_Click(object sender, EventArgs e)
        {
            double s, n;
            n = Convert.ToDouble(txtResult.Text);
            s = Math.Sqrt(n);
            txtResult.Text = s.ToString();
        }

        //求平方数
        private void btnSquare_Click(object sender, EventArgs e)
        {
            double s, n;
            n = Convert.ToDouble(txtResult.Text);
            s = Math.Pow(n, 2);
            txtResult.Text = s.ToString();
        }

        //求倒数
        private void btnReciprocal_Click(object sender, EventArgs e)
        {
            double s, n;
            n = Convert.ToDouble(txtResult.Text);
            if (n == 0)
            {
                MessageBox.Show("无法求0的倒数！", "错误");
                txtResult.Focus();
            }
            else
            {
                s = 1 / n;
                txtResult.Text = s.ToString();
            }
        }

        //正负号
        private void btnSign_Click(object sender, EventArgs e)
        {
            if (txtResult.Text.Contains('-'))
            {
                txtResult.Text = txtResult.Text.Substring(1);
            }
            else
            {
                txtResult.Text = "-" + txtResult.Text;
            }
        }

        //小数点
        private void btnPoint_Click(object sender, EventArgs e)
        {

            if (!txtResult.Text.Contains('.'))
                txtResult.Text += btnPoint.Text;
        }

        //获取操作数op1
        private void getOp1()
        {
            try
            {
                op1 = Convert.ToDouble(txtResult.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                txtResult.Text = "0";
                return;
            }
        }

        //加法运算
        private void btnAdd_Click(object sender, EventArgs e)
        {
            ch = '+';
            getOp1();
            txtResult2.Text = op1.ToString();
            txtResult.SendToBack();
            txtResult2.BringToFront();
            txtResult.Text = "0";
            t = 0;
        }

        //减法运算
        private void btnSub_Click(object sender, EventArgs e)
        {
            ch = '-';
            getOp1();
            txtResult2.Text = op1.ToString();
            txtResult.SendToBack();
            txtResult2.BringToFront();
            txtResult.Text = "0";
            t = 0;
        }

        //乘法运算
        private void btnMul_Click(object sender, EventArgs e)
        {
            ch = '×';
            getOp1();
            txtResult2.Text = op1.ToString();
            txtResult.SendToBack();
            txtResult2.BringToFront();
            txtResult.Text = "0";
            t = 0;
        }

        //除法运算
        private void btnDiv_Click(object sender, EventArgs e)
        {
            ch = '÷';
            getOp1();
            txtResult2.Text = op1.ToString();
            txtResult.SendToBack();
            txtResult2.BringToFront();
            txtResult.Text = "0";
            t = 0;
        }

        //取余
        private void btnPercent_Click(object sender, EventArgs e)
        {
            ch = '%';
            getOp1();
            txtResult2.Text = op1.ToString();
            txtResult.SendToBack();
            txtResult2.BringToFront();
            txtResult.Text = "0";
            t = 0;
        }

        int t = 0;
        //结果
        private void btnResult_Click(object sender, EventArgs e)
        {
            op2 = Convert.ToDouble(txtResult.Text);
            switch (ch)
            {
                case '+':
                    txtResult.Text = (op1 + op2).ToString();
                    break;
                case '-':
                    txtResult.Text = (op1 - op2).ToString();
                    break;
                case '×':
                    txtResult.Text = (op1 * op2).ToString();
                    break;
                case '÷':
                    if (op2 == 0)
                    {
                        MessageBox.Show("除数不能为0!", "错误");
                        txtResult.Focus();
                    }
                    else
                        txtResult.Text = (op1 / op2).ToString();
                    break;
                case '%':
                    txtResult.Text = (op1 % op2).ToString();
                    break;
            }
            if (t == 0) op1 = op2;
            t++;
        }
    }
}
